package com.service.scheduler.sched;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;

import com.service.cache.CacheManager;
import com.service.other.Logger;
import com.service.scheduler.job.SaveGpsJob;
import com.service.scheduler.server.SchedulerCreator;

public class SaveGpsSched {
	
	private Scheduler sched;
	
	public void init() {
		try {
			// 默认优先级5，数字越大，优先级越高
			sched = SchedulerCreator.createScheduler(SaveGpsSched.class.getSimpleName(), 1, Trigger.DEFAULT_PRIORITY);
			initSaveGps();
			CacheManager.instance().schedMap.put(sched.getSchedulerName(),sched);
		} catch (SchedulerException e) {
			Logger.err(e);
		}
	}
	
	/**
	 * 将Gps数据保存到数据库(每20秒执行一次)
	 * @throws SchedulerException
	 */
	private void initSaveGps() throws SchedulerException {
		JobKey jobKey = SchedulerCreator.createJobKey("saveGps", "com.service.cache");
		TriggerKey triggerKey = SchedulerCreator.createTriggerKey("saveGps","com.service.cache");
		String cronExpr = SchedulerCreator.createSecondExpr(20);
		JobDetail job = SchedulerCreator.createJob(jobKey, SaveGpsJob.class);
		CronTrigger cronTrigger = SchedulerCreator.createCronTrigger(triggerKey,cronExpr);
		sched.scheduleJob(job, cronTrigger);
	}
	
}
